package com.lagou.minicat.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName HttpProtocloUtil
 * @Description http协议工具类
 * @Author playboy
 * @Date 2021/5/22 3:55 下午
 * @Version 1.0
 **/
public class HttpProtocloUtil {
	private static Logger log = LoggerFactory.getLogger(HttpProtocloUtil.class);


	public static byte[] getHeader200(String context) {
		String response = "HTTP/1.1 200 OK\n" +
				"Content-Length: " + context.getBytes(StandardCharsets.UTF_8).length + "\n" +
				"Content-Type: text/plain; charset=UTF-8\n" +
				"\n"
				+ context;
		return response.getBytes(StandardCharsets.UTF_8);
	}

	public static byte[] getHeader200(int byteCount) {
		String response = "HTTP/1.1 200 OK\n" +
				"Content-Length: " + byteCount + "\n" +
				"Content-Type: text/html; charset=UTF-8\n" +
				"\n";
		return response.getBytes(StandardCharsets.UTF_8);
	}

	public static byte[] get404() {
		String result = "未找到资源";
		String response = "HTTP/1.1 404 Not Found\n" +
				"Content-Length: " + result.getBytes(StandardCharsets.UTF_8).length + "\n" +
				"Content-Type: text/plain; charset=UTF-8\n" +
				"\n"
				+ result;
		return response.getBytes(StandardCharsets.UTF_8);
	}
}
